<template>
  <div>
    <el-card shadow="never">
      <el-page-header @back="goBack" content="组织机构详情"></el-page-header>
    </el-card>
    <el-row>
      <el-col :span="16">
        <el-row>
          <el-card>
            <el-descriptions title="组织机构信息" :column="3"  border>
              <template slot="extra">
                <span style="margin-right: 10px;">
                  <el-button
                    size="mini"
                    type="danger"
                    @click.stop="handleDelete()"
                    v-hasPermi="['system:dept:remove']"
                  >解散</el-button>
                </span>
                <span>
                  <el-button
                    size="mini"
                    type="primary"
                    @click.stop="handleUpdate()"
                    v-hasPermi="['system:dept:edit']"
                  >修改</el-button>
                </span>
              </template>
              <el-descriptions-item label="组织机构名称">{{orgDetail.organizationName}}</el-descriptions-item>
              <el-descriptions-item label="组织机构编码">{{orgDetail.organizationCode}}</el-descriptions-item>
              <el-descriptions-item label="组织机构类型">{{orgDetail.organizationTypeName}}</el-descriptions-item>
              <el-descriptions-item label="组织机构负责人">{{orgDetail.organizationPrincipalName}}</el-descriptions-item>
              <el-descriptions-item label="是否虚拟组织">{{orgDetail.organizationIsVirtual==='0'?'否':'是'}}</el-descriptions-item>
              <el-descriptions-item label="创建人">{{orgDetail.createBy}}</el-descriptions-item>
              <el-descriptions-item label="创建时间">{{orgDetail.createTime}}</el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-row>
        <el-row>
          <el-card class="employee-panel">
            <el-tabs v-model="activeName" @tab-click="changeTab">
                <el-tab-pane label="所有员工" name="all">
                  <el-table :data="allChildEmployees" >
                    <el-table-column prop="employeeName" label="姓名" align="center"></el-table-column>
                    <el-table-column prop="employeeCode" label="工号" align="center"></el-table-column>
                    <el-table-column prop="postName" label="岗位" width="200" align="center"></el-table-column>
                    <el-table-column prop="employeeTel" label="手机号" width="200" align="center"></el-table-column>
                  </el-table>
                  <el-pagination
                    layout="prev, pager, next"
                    :total="allTotal">
                  </el-pagination>
                </el-tab-pane>
                <el-tab-pane label="专职员工" name="fullJob">
                  <el-table :data="fullEmployees" >
                    <el-table-column prop="employeeName" label="姓名" align="center"></el-table-column>
                    <el-table-column prop="employeeCode" label="工号" align="center"></el-table-column>
                    <el-table-column prop="postName" label="岗位" width="200" align="center"></el-table-column>
                    <el-table-column prop="employeeTel" label="手机号" width="200" align="center"></el-table-column>
                  </el-table>
                  <el-pagination
                    layout="prev, pager, next"
                    :total="fullTotal">
                  </el-pagination>
                </el-tab-pane>
                <el-tab-pane label="兼职员工" name="partJob">
                  <el-table :data="partEmployees" >
                    <el-table-column prop="employeeName" label="姓名" align="center"></el-table-column>
                    <el-table-column prop="employeeCode" label="工号" align="center"></el-table-column>
                    <el-table-column prop="postName" label="岗位" width="200" align="center"></el-table-column>
                    <el-table-column prop="employeeTel" label="手机号" width="200" align="center"></el-table-column>
                  </el-table>
                  <el-pagination
                    layout="prev, pager, next"
                    :total="partTotal">
                  </el-pagination>
                </el-tab-pane>
              </el-tabs>
          </el-card>
        </el-row>
      </el-col>
      <el-col :span="8">
        <el-card class="orgaization-tree-container">
          <div slot="header" class="clearfix">
            <span>卡片名称</span>
          </div>
          <VueOrgTree :data="orgTreeData" :props="orgTreeProps" :collapsable="true"></VueOrgTree>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑组件 -->
    <edit v-if="detailEditOpen" :prop="editData" @close="closeEdit"></edit>
  </div>
</template>

<script>
  import VueOrgTree from 'vue2-org-tree'
  import {getDetail,del,getOrganizationAllChildEmployees,getOrganizationFullJobEmployees,getOrganizationPartJobEmployees,getAllChildOrganization, } from '@/api/organization/organization/organization.js'
  import edit from '@/views/organization/organization/edit.vue'
  export default {
    name: "OrganizationDetail",
    dicts: [],
    components: {VueOrgTree,edit},
    props:{
      prop:{
        type:Object,
        default:function(){
          return {
            id:null
          }
        }
      }
    },
    data() {
      return {
        orgId:this.prop.id,
        orgDetail:{
          organizationName:null,
          organizationCode:null,
          organizationSysCode:null,
          organizationTypeName:null,
          organizationPrincipalName:null,
          organizationIsVirtual:null,
          createBy:null,
          createTime:null,
          remark:null,
        },
        activeName:'all',
        allChildEmployees:[],
        fullEmployees:[],
        partEmployees:[],
        orgTreeData:{},
        orgTreeProps:{label: 'organizationName', children: 'children', expand: 'expand'},
        //编辑组件数据
        // 遮罩层
        loading: false,
        detailEditOpen:false,
        editData:{
          title:'修改',
          parentId:null,
          editId:null,
          isRoot:false,
          open:false,
        },
        allTotal:0,
        allParams:{
          size:10,
          current:1,
          organizationId:null
        },
        fullTotal:0,
        fullParams:{
          size:10,
          current:1,
          organizationId:null,
          employeeEmployForm:'0',
        },
        partTotal:0,
        partParams:{
          size:10,
          current:1,
          organizationId:null,
          employeeEmployForm:'1',
        }
      };
    },
    methods: {
      getPreLoad(){
        this.allParams.organizationId = this.orgId
        let preMethods = [getDetail({organizationId:this.orgId}),getOrganizationAllChildEmployees(this.allParams)]
        Promise.all(preMethods).then(resArr =>{
          this.orgDetail = resArr[0].data
          this.allChildEmployees = resArr[1].data.records
          let orgTreeData = this.handleTree(resArr[2].data, "organizationId","organizationParentId")
          if(orgTreeData!==null && orgTreeData.length>0){
            this.orgTreeData = orgTreeData[0]
          }
        }).catch(err =>{
          console.log('查询组织机构详情失败，原因：',err)
        })
      },
      getAllChildEmployees(){
        this.allParams.organizationId = this.orgId
        getOrganizationAllChildEmployees(this.allParams).then(res =>{
          this.allChildEmployees = res.data.records
        }).catch(err =>{
          console.log('查询组织机构详情失败，原因：',err)
        })
      },
      getFullEmployees(){
        this.fullParams.organizationId = this.orgId
        getOrganizationFullJobEmployees(this.fullParams).then(res =>{
           this.fullEmployees = res.data.records
        }).catch(err =>{
          console.log('查询组织机构详情失败，原因：',err)
        })
      },
      getPartEmployees(){
        this.partParams.organizationId = this.orgId
        getOrganizationPartJobEmployees(this.partParams).then(res =>{
           this.partEmployees = res.data.records
           this.partTotal = res.data.total
        }).catch(err =>{
          console.log('查询组织机构详情失败，原因：',err)
        })
      },
      changeTab(selectTab){
        if(this.activeName === 'all'){
          this.getAllChildEmployees()
        }else if(this.activeName === 'fullJob'){
          this.getFullEmployees()
        }else{
          this.getPartEmployees()
        }
      },
      handleView(row){
        let employeeId = row.employeeId
        this.$message({
          message: '敬请期待',
          type: 'warning'
        });
      },
      /** 删除按钮操作 */
      handleDelete() {
        this.$confirm('是否删除该组织机构？','确认信息',{
          distinguishCancelAndClose: true,
          confirmButtonText: '确认',
          cancelButtonText: '取消'
        }).then(()=>{
          del({organizationId:this.orgDetail.organizationId,organizationSysCode:this.orgDetail.organizationSysCode}).then(res =>{
            if(res.code === 200){
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.getList()
            }else{
              this.$message({
                type: 'warning',
                message: res.msg||'该组织机构已被使用，无法删除'
              })
            }
          })
        }).catch(action => {

        });
      },
      /** 修改按钮操作 */
      handleUpdate() {
        if(this.orgDetail.organizationParentId === "0"){
          this.editData.isRoot = true
        }
        this.editData.parentId =this.orgDetail.organizationParentId;
        this.editData.title = "修改";
        this.editData.editId = this.orgDetail.organizationId
        this.editData.open = true
        this.detailEditOpen = true;
      },
      closeEdit(){
        this.detailEditOpen = false
        this.editData.open = false
        this.getList()
      },
      goBack(){
        this.$emit('close')
      },
    },
    mounted() {
      this.getPreLoad()
    },
  };
</script>
<style scoped>
  >>> .org-tree-container {
    display: inline-block;
    padding: 15px;
    background-color: #fff;
  }
  >>> .org-tree {
    display: table;
    text-align: center;
  }
  >>> .org-tree:before,
  >>> .org-tree:after {
    content: '';
    display: table;
  }
  >>> .org-tree:after {
    clear: both;
  }
  >>> .org-tree-node,
  >>> .org-tree-node-children {
    position: relative;
    margin: 0;
    padding: 0;
    list-style-type: none;
  }
  >>> .org-tree-node:before,
  >>> .org-tree-node-children:before,
  >>> .org-tree-node:after,
  >>> .org-tree-node-children:after {
    transition: all .35s;
  }
  >>> .org-tree-node-label {
    position: relative;
    display: inline-block;
  }
  >>> .org-tree-node-label .org-tree-node-label-inner {
    padding: 10px 15px;
    text-align: center;
    border-radius: 3px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.15);
  }
  >>> .org-tree-node-btn {
    position: absolute;
    top: 100%;
    left: 50%;
    width: 20px;
    height: 20px;
    z-index: 10;
    margin-left: -11px;
    margin-top: 9px;
    background-color: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    transition: all .35s ease;
  }
  >>> .org-tree-node-btn:hover {
    background-color: #e7e8e9;
    transform: scale(1.15);
  }
  >>> .org-tree-node-btn:before,
  >>> .org-tree-node-btn:after {
    content: '';
    position: absolute;
  }
  >>> .org-tree-node-btn:before {
    top: 50%;
    left: 4px;
    right: 4px;
    height: 0;
    border-top: 1px solid #ccc;
  }
  >>> .org-tree-node-btn:after {
    top: 4px;
    left: 50%;
    bottom: 4px;
    width: 0;
    border-left: 1px solid #ccc;
  }
  >>> .org-tree-node-btn.expanded:after {
    border: none;
  }
  >>> .org-tree-node {
    padding-top: 20px;
    display: table-cell;
    vertical-align: top;
  }
  >>> .org-tree-node.is-leaf,
  >>> .org-tree-node.collapsed {
    padding-left: 10px;
    padding-right: 10px;
  }
  >>> .org-tree-node:before,
  >>> .org-tree-node:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 19px;
  }
  >>> .org-tree-node:after {
    left: 50%;
    border-left: 1px solid #ddd;
  }
  >>> .org-tree-node:not(:first-child):before,
  >>> .org-tree-node:not(:last-child):after {
    border-top: 1px solid #ddd;
  }
  >>> .collapsable .org-tree-node.collapsed {
    padding-bottom: 30px;
  }
  >>> .collapsable .org-tree-node.collapsed .org-tree-node-label:after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0;
    width: 50%;
    height: 20px;
    border-right: 1px solid #ddd;
  }
  >>> .org-tree > .org-tree-node {
    padding-top: 0;
  }
  >>> .org-tree > .org-tree-node:after {
    border-left: 0;
  }
  >>> .org-tree-node-children {
    padding-top: 20px;
    display: table;
  }
  >>> .org-tree-node-children:before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    width: 0;
    height: 20px;
    border-left: 1px solid #ddd;
  }
  >>> .org-tree-node-children:after {
    content: '';
    display: table;
    clear: both;
  }
  >>> .horizontal .org-tree-node {
    display: table-cell;
    float: none;
    padding-top: 0;
    padding-left: 20px;
  }
  >>> .horizontal .org-tree-node.is-leaf,
  >>> .horizontal .org-tree-node.collapsed {
    padding-top: 10px;
    padding-bottom: 10px;
  }
  >>> .horizontal .org-tree-node:before,
  >>> .horizontal .org-tree-node:after {
    width: 19px;
    height: 50%;
  }
  >>> .horizontal .org-tree-node:after {
    top: 50%;
    left: 0;
    border-left: 0;
  }
  >>> .horizontal .org-tree-node:only-child:before {
    top: 1px;
    border-bottom: 1px solid #ddd;
  }
  >>> .horizontal .org-tree-node:not(:first-child):before,
  >>> .horizontal .org-tree-node:not(:last-child):after {
    border-top: 0;
    border-left: 1px solid #ddd;
  }
  >>> .horizontal .org-tree-node:not(:only-child):after {
    border-top: 1px solid #ddd;
  }
  >>> .horizontal .org-tree-node .org-tree-node-inner {
    display: table;
  }
  >>> .horizontal .org-tree-node-label {
    display: table-cell;
    vertical-align: middle;
  }
  >>> .horizontal.collapsable .org-tree-node.collapsed {
    padding-right: 30px;
  }
  >>> .horizontal.collapsable .org-tree-node.collapsed .org-tree-node-label:after {
    top: 0;
    left: 100%;
    width: 20px;
    height: 50%;
    border-right: 0;
    border-bottom: 1px solid #ddd;
  }
  >>> .horizontal .org-tree-node-btn {
    top: 50%;
    left: 100%;
    margin-top: -11px;
    margin-left: 9px;
  }
  >>> .horizontal > .org-tree-node:only-child:before {
    border-bottom: 0;
  }
  >>> .horizontal .org-tree-node-children {
    display: table-cell;
    padding-top: 0;
    padding-left: 20px;
  }
  >>> .horizontal .org-tree-node-children:before {
    top: 50%;
    left: 0;
    width: 20px;
    height: 0;
    border-left: 0;
    border-top: 1px solid #ddd;
  }
  >>> .horizontal .org-tree-node-children:after {
    display: none;
  }
  >>> .horizontal .org-tree-node-children > .org-tree-node {
    display: block;
  }
  >>> .employee-panel{
    height: 65vh;
    margin-top: 10px;
  }
  >>> .employee-panel .el-card__body{
    height: 65vh;
  }
  >>> .employee-panel .el-card__body .el-tabs__content{
    height: 60vh;
  }
  >>>.orgaization-tree-container{
    height: 80vh;
    width: calc(100% - 10px);
    margin-left: 10px;
  }
  >>> .orgaization-tree-container .el-card__body{
    height: 80vh;
    justify-content: center;
    display: flex;
  }
</style>
